package com.sun.controller;

import com.sun.entity.BUser;
import com.sun.service.BUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * (BUser)表控制层
 *
 * @author makejava
 * @since 2021-01-07 16:29:23
 */
@Controller
@RequestMapping("/admin")
public class BUserController {
    /**
     * 服务对象
     */
    @Resource
    private BUserService bUserService;

    @GetMapping
    public String loginPage(){
        return "admin/login";
    }
    @PostMapping("/login")
    public String login(String username, String password, HttpSession session,
                        RedirectAttributes redirectAttributes){

        BUser user = bUserService.checkUser(username, password);
        if (user != null){
            user.setUserPwd(null);
            session.setAttribute("user", user.getUserName());
            return "admin/index";
        }else {
            redirectAttributes.addFlashAttribute("message", "用户名密码错误");
            return "redirect:/admin";
        }

    }
    @GetMapping("/logOut")
    public String logOut(HttpSession session){
        session.removeAttribute("username");
        return "redirect:/admin";
    }


}